package com.zhongyouyun.wx.wxapplet.cqyc.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhongyouyun.cqyc.model.NoticeModel;
import com.zhongyouyun.cqyc.model.NoticeTypeModel;
import com.zhongyouyun.cqyc.pojo.NoticePojo;
import com.zhongyouyun.cqyc.service.NoticeService;
import com.zhongyouyun.cqyc.service.NoticeTypeService;
import com.zhongyouyun.other.model.PageRequest;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.utils.QueryUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: shenyi
 * @Date: 2023-08-01
 * @Description:
 */
@RestController
@RequestMapping("wx/wxapplet/cqyc/notice")
@Api(value = "烟草城区-微信小程序公告操作", tags = {"烟草城区-微信小程序公告操作"})
public class WxAppletNoticeController {
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private NoticeTypeService noticeTypeService;

    @GetMapping("/findList")
    @ResponseBody
    @ApiOperation(value = "公告内容类型列表", notes = "公告内容类型列表")
    public Result<List<NoticeTypeModel>> findList() {
        QueryWrapper<NoticeTypeModel> queryWrapper = new QueryWrapper();
        List<NoticeTypeModel> list = noticeTypeService.selectList(queryWrapper);
        if (null != list && list.size() > 0) {
            return Result.<List<NoticeTypeModel>>builder().message("成功").status(0).data(list).build();
        } else {
            return Result.<List<NoticeTypeModel>>builder().status(0).build();
        }
    }

    @GetMapping("/findTPList")
    @ResponseBody
    @ApiOperation(value = "公告获取带图片的前5条数据", notes = "公告获取带图片的前5条数据")
    public Result findTPList() {
        QueryWrapper<NoticeModel> queryWrapper = new QueryWrapper();
        queryWrapper.eq("state","1");
        queryWrapper.ne("url","");
        queryWrapper.isNotNull("url");
        queryWrapper.orderByDesc("createBy");
        queryWrapper.last("LIMIT 5");
        List<NoticeModel> noticeModels = noticeService.selectList(queryWrapper);
        List<Map> maps = new ArrayList<>();
        for (NoticeModel noticeModel:noticeModels) {
            String substring = delHTMLTag(noticeModel.getContext());
            if(substring.length() > 60){
                substring = substring.substring(0,59)+"...";
            }
            noticeModel.setContext(substring);
            NoticeTypeModel noticeTypeModel = noticeTypeService.getById(noticeModel.getTypeId());
            Map map = new HashMap();
            map.put("noticeModel",noticeModel);
            map.put("noticeTypeModel",noticeTypeModel);
            maps.add(map);
        }
        return Result.builder().message("成功").status(0).data(maps).build();
    }

    @GetMapping("/findPage")
    @ResponseBody
    @ApiOperation(value = "公告分页列表", notes = "公告分页列表")
    public IPage<NoticePojo> findPage(@ApiParam(name = "当前第几页", value = "currentPage", required = true) @RequestParam(value = "currentPage") int currentPage,
                                      @ApiParam(name = "一页查几个", value = "pageSize", required = true) @RequestParam(value = "pageSize") int pageSize, @ApiParam(name = "类型ID", value = "typeId", required = false) @RequestParam(value = "typeId",required = false) String typeId) {
        QueryWrapper<NoticePojo> queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(typeId)) {
            queryWrapper.eq("type_id", typeId);
        }
        queryWrapper.eq("state","1");
        Page page = new Page(currentPage, pageSize);
        IPage<NoticePojo> noticePojoIPage = noticeService.selectPojoPage(page, queryWrapper);
        List<NoticePojo> records = noticePojoIPage.getRecords();
        for (NoticePojo noticePojo:records) {
            String substring = delHTMLTag(noticePojo.getContext());
            if(substring.length() > 60){
                substring = substring.substring(0,59)+"...";
            }
            noticePojo.setContext(substring);
        }
        IPage<NoticePojo> ipage = new Page<>();
        ipage.setRecords(records);
        return ipage;
    }

    @GetMapping("/getDetails")
    @ResponseBody
    @ApiOperation(value = "获取公告信息", notes = "获取公告信息")
    public Result getDetails(@ApiParam(name = "口袋书ID", value = "id", required = true) @RequestParam(value = "id") String id) {
        NoticeModel noticeModel = noticeService.getById(id);
        if (ObjectUtil.isNotEmpty(noticeModel)) {
            return Result.builder().message("成功").status(0).data(noticeModel).build();
        } else {
            return Result.builder().message("获取失败").success(false).status(1).build();
        }
    }
    public static String delHTMLTag(String htmlStr){
        String regEx_script="<script[^>]*?>[\\s\\S]*?<\\/script>"; //定义script的正则表达式
        String regEx_style="<style[^>]*?>[\\s\\S]*?<\\/style>"; //定义style的正则表达式
        String regEx_html="<[^>]+>"; //定义HTML标签的正则表达式

        Pattern p_script=Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
        Matcher m_script=p_script.matcher(htmlStr);
        htmlStr=m_script.replaceAll(""); //过滤script标签

        Pattern p_style=Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
        Matcher m_style=p_style.matcher(htmlStr);
        htmlStr=m_style.replaceAll(""); //过滤style标签

        Pattern p_html=Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
        Matcher m_html=p_html.matcher(htmlStr);
        htmlStr=m_html.replaceAll(""); //过滤html标签

        return htmlStr.trim(); //返回文本字符串
    }
}
